package it.unibo.android.convoycruisecontrol.boatsubsystem;

import java.io.File;

import dalvik.system.DexClassLoader;

import android.app.Activity;
import android.view.ViewGroup;
import it.unibo.contact.boatSubSystem.BoatSubSystemLauncher;
import it.unibo.convoycruisecontrol.boatsubsystem.IBoatFactory;
import it.unibo.convoycruisecontrol.boatsubsystem.ObservableDashboard;
import it.unibo.convoycruisecontrol.progettodesignandroid.R;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.id;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.layout;
import it.unibo.location.LocationDevice;

import it.unibo.android.location.*;

public class AndroidBoatSubSystemLauncher extends BoatSubSystemLauncher 
{
	Activity activity;
	
	public AndroidBoatSubSystemLauncher(Activity activity)
	{
		this.activity = activity;
	}

	@Override
	protected void initGui() 
	{
		activity.setContentView(R.layout.main);		

		DashboardView view = new DashboardView(activity, (ObservableDashboard) dashboard);
		MyMapView viewMap = new MyMapView(activity, (ObservableDashboard) dashboard);

		ViewGroup container = (ViewGroup) activity.findViewById(R.id.myLayout);

		container.addView(view);
		container.addView(viewMap);
		
	}

	@Override
	protected IBoatFactory getBoatFactory(String className) 
	{
		String[] data = className.split(":");
		
		final String path = "/data/convoy/" + data[0];
	    
	    File dexOutputDir = activity.getApplicationContext().getDir("dex", 0);
	    DexClassLoader myClassLoader2 = new DexClassLoader(path, dexOutputDir.getAbsolutePath(), null, activity.getClassLoader());
	    
	    IBoatFactory boatFactory = null;
		try 
		{
			Class<?> clazz = myClassLoader2.loadClass(data[1]);
			boatFactory = (IBoatFactory)clazz.newInstance();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	    
		return boatFactory;
	}

	@Override
	protected LocationDevice getLocationDevice(String deviceName) 
	{
		AndroidLocationDeviceFactory factory = new AndroidLocationDeviceFactory();
		return factory.createDeviceByName(deviceName);
	}

}
